//
//  sdl_ffplay_struct.h
//  FFmpegDemo
//
//  Created by xiaerfei on 2019/7/23.
//  Copyright © 2019 erfeixia. All rights reserved.
//

#ifndef sdl_ffplay_struct_h
#define sdl_ffplay_struct_h

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/avstring.h>
#include <libavutil/time.h>
#include <SDL2/SDL.h>
#include "sdl_queue/sdl_queue.h"
#include "sdl_ffplay_clock.h"

enum {
    AV_SYNC_AUDIO_MASTER, /* default choice */
    AV_SYNC_VIDEO_MASTER,
    AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
};

typedef struct AudioParams {
    int freq;
    int channels;
    int64_t channel_layout;
    enum AVSampleFormat fmt;
    int frame_size;
    int bytes_per_sec;
} AudioParams;

typedef struct Decoder {
    AVPacket pkt;
    PacketQueue *queue;
    AVCodecContext *avctx;
    int pkt_serial;
    int finished;
    int packet_pending;
    SDL_cond *empty_queue_cond;
    int64_t start_pts;
    AVRational start_pts_tb;
    int64_t next_pts;
    AVRational next_pts_tb;
    SDL_Thread *decoder_tid;
} Decoder;


typedef struct VideoState {
    
    SDL_Thread *read_thread;
    
    AVInputFormat *iformat;
    AVFormatContext *ic;
    
    
    int video_stream_index;
    int audio_stream_index;
    int sub_title_stream_index;
    
    Clock audclk;
    Clock vidclk;
    Clock extclk;
    
    double frame_last_delay;
    double frame_timer;
    
    FrameQueue pictq;
    FrameQueue subpq;
    FrameQueue sampq;
    
    PacketQueue videoq;
    PacketQueue audioq;
    PacketQueue subtitleq;
    
    AVStream *video_stream;
    AVStream *audio_stream;
    
    Decoder viddec;
    Decoder auddec;
    
    struct AudioParams audio_src;
    struct AudioParams audio_tgt;
    
    int audio_clock_serial;
    int av_sync_type;
    
    uint8_t *audio_buf;
    uint8_t *audio_buf1;
    int audio_hw_buf_size;
    unsigned int audio_buf_size; /* in bytes */
    unsigned int audio_buf1_size;
    int audio_buf_index; /* in bytes */
    int audio_write_buf_size;
    int audio_volume;
    int muted;
    
    double audio_clock;
    double max_frame_duration;
    int seek_req;
    int seek_flags;
    int64_t seek_pos;
    int64_t seek_rel;
    
    struct SwrContext *swr_ctx;
    
    char *filename;
    int width, height, xleft, ytop;
    SDL_cond *continue_read_thread;
    
    SDL_Texture *vis_texture;
    SDL_Texture *sub_texture;
    SDL_Texture *vid_texture;
    
    struct SwsContext *img_convert_ctx;
    struct SwsContext *sub_convert_ctx;
    
    int eof;
    
} VideoState;



#endif /* sdl_ffplay_struct_h */
